﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using 安检系统.Model;
using 安检系统.Utils;
using 安检系统.ViewModel;

namespace 安检系统
{
    /// <summary>
    /// StandardWindow.xaml 的交互逻辑
    /// </summary>
    public partial class StandardWindow : Window
    {
        Record record;
        StandardEditViewmodel viewmodel;
        public String carNumber = "";
        public String recordId = "";
        public String vehiclecode = "";
        public String pickerTime = "";
        public String Num = "";
        public StandardWindow()
        {
            InitializeComponent();
            viewmodel = new StandardEditViewmodel();
            record = new Record();
            viewmodel.record = record;
            this.DataContext = viewmodel;

            this.Loaded += Window_Load;
        }

        private void Window_Load(object sender, RoutedEventArgs e)
        {
            String standardStr = App.GetBaseInfo().Effluent_Standard;
            if (standardStr != null)
            {
                String[] standards = standardStr.Split(',');
                foreach (String s in standards)
                {
                    viewmodel.standards.Add(s);
                }
            }
            foreach (Material m in App.getConfig().materials)
            {
                viewmodel.materials.Add(m);
            }
            vehicleCode.Text = vehiclecode;
            pickerReportDate.Text = pickerTime;
            num.Text = Num;
        }

        private void Button_Click_Close(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void changeStandard(object sender, RoutedEventArgs e)
        {
            RadioButton button = sender as RadioButton;
            String val = (String)button.Tag;
            if (val == null) return;
            String txt = record.materialName;
            if (txt==null||txt.Equals(""))
            {
                MessageBox.Show("运输货物名称为空");
                return;
            }
            try
            {
                
                JObject paramJSON = new JObject();
                paramJSON.Add("carNumber", carNumber);
                paramJSON.Add("carEffluentStandard", val);
                paramJSON.Add("id", recordId);
                paramJSON.Add("vehicleCode", vehicleCode.Text);
                if (pickerReportDate.SelectedDate!=null)
                {
                    paramJSON.Add("registrationTime", pickerReportDate.SelectedDate.ToString());
                }
                paramJSON.Add("materialName", record.materialName);
                paramJSON.Add("num",num.Text);
                String response = HttpPostHelper.doHttpPost("/parkRecord/upadteCarStandard", paramJSON.ToString());
                JObject respJson = JObject.Parse(response);
                bool success = (bool)respJson["success"];
                if (success)
                {
                    this.DialogResult = true;
                    this.Close();
                    
                }
                else
                {
                    String message = respJson["message"].ToString();
                    MessageBox.Show(message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("系统错误,请联系管理员");
            }
        }
    }
}
